package com.demo.mqtt.zigbee.entity.po.eigenUpload;

import com.demo.mqtt.common.utils.AssistUtil;
import com.demo.mqtt.common.utils.IotStringUtil;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: mqtt
 * @description: 特征值上传原始数据
 * @author: liuYunKai
 * @create: 2021-03-26 13:13
 */
@Data
@Document(collection = "zigbee_eigen_upload")
public class EigenUpLoadPO {
    @Id
    private String id;
    //SN序列号
    private String sn;
    //特征值时间
    private String eigenDate;
    private String eigenTime;
    //参数时间
    private String paramDate;
    private String paramTime;
    //RSSI
    private byte rssi;
    //电池电压值
    private Float voltage;
    //温度值
    private Float temperature;

    private List<EigenDirecData> stEigenDirList;


    public EigenUpLoadPO(EigenUpLoad eigenUpLoad, ByteBuf byteBuf) {
        this.sn = eigenUpLoad.SN.get();

        LocalDateTime eigentile = IotStringUtil.getTimeFromUnsigned8(eigenUpLoad.eigenTime);
        this.eigenDate = eigentile.toLocalDate().toString();
        this.eigenTime = eigentile.toLocalTime().toString();
        LocalDateTime paramTime = IotStringUtil.getTimeFromUnsigned8(eigenUpLoad.paramTime);
        this.paramDate = paramTime.toLocalDate().toString();
        this.paramTime = paramTime.toLocalTime().toString();
        this.rssi = eigenUpLoad.rssi.get();
        this.voltage = eigenUpLoad.voltage.get();
        this.temperature = eigenUpLoad.temperature.get();

        //特征值
        byteBuf.skipBytes(29);

        /**----------- 第一组特征值------------*/
        List<EigenDirecData> eigenDirecDataList = new ArrayList<>();
        EigenDirecData direcData = new EigenDirecData();
        direcData.setDirection(AssistUtil.readByteBuf(byteBuf, 1));
        direcData.setHighTotal(AssistUtil.readByteBuf(byteBuf, 4));
        direcData.setLowTotal(AssistUtil.readByteBuf(byteBuf, 4));
        direcData.setSpeedTotal(AssistUtil.readByteBuf(byteBuf, 4));
        direcData.setAlarmStatus(AssistUtil.readByteBuf(byteBuf, 1));

        eigenDirecDataList.add(direcData);
        /**----------- 第二组特征值------------*/
        // 进行有无的判断
        if(!(byteBuf.writerIndex() == byteBuf.readerIndex()) ){
            EigenDirecData direcData2 = new EigenDirecData();
            direcData2.setDirection(AssistUtil.readByteBuf(byteBuf, 1));
            direcData2.setHighTotal(AssistUtil.readByteBuf(byteBuf, 4));
            direcData2.setLowTotal(AssistUtil.readByteBuf(byteBuf, 4));
            direcData2.setSpeedTotal(AssistUtil.readByteBuf(byteBuf, 4));
            direcData2.setAlarmStatus(AssistUtil.readByteBuf(byteBuf, 1));

            eigenDirecDataList.add(direcData2);
            /**----------- 第三组特征值------------*/
            // 进行有无的判断
            if(!(byteBuf.writerIndex() == byteBuf.readerIndex()) ){
                EigenDirecData direcData3 = new EigenDirecData();
                direcData3.setDirection(AssistUtil.readByteBuf(byteBuf, 1));
                direcData3.setHighTotal(AssistUtil.readByteBuf(byteBuf, 4));
                direcData3.setLowTotal(AssistUtil.readByteBuf(byteBuf, 4));
                direcData3.setSpeedTotal(AssistUtil.readByteBuf(byteBuf, 4));
                direcData3.setAlarmStatus(AssistUtil.readByteBuf(byteBuf, 1));

                eigenDirecDataList.add(direcData3);
            }
        }
        this.stEigenDirList = eigenDirecDataList;

    }
}
